package com.mmp.main.servlet.joborder;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mmp.common.bo.Message;
import com.mmp.common.util.Common;
import com.mmp.common.util.FileUtil;
import com.mmp.main.entity.JobOrder;
import com.mmp.main.util.Enums;
import com.mmp.main.util.WebUtil;

/**
 * Servlet implementation class JobOrderFollowupFirstPhaseProcessServlet
 */
@MultipartConfig
public class JobOrderFollowupFirstPhaseProcessServlet extends JobOrderTrackingProcessServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		int jobOrderId = WebUtil.getParamValueAsInteger(request, "jobOrderId", 0);

		JobOrder order = dbQueryManager.findById(jobOrderId, JobOrder.class);

		String[] joRequestFormUrl = FileUtil.uploadFiles(request, "joRequestFormUrl");
		// if (joRequestFormUrl != null && joRequestFormUrl.length != 0)
		order.setRequestFormUrl(Common.concatenateValues(joRequestFormUrl));

		order.setFirstAction(WebUtil.getParamValue(request, "firstAction", null));
		order.setFirstActionDate(WebUtil.getParamValueAsDate(request, "firstActionDate", null));

		// To avoid phase updating in previous phases updating mode
		if (order.getLastCompletedPhaseId() == Enums.JOBORDER_FOLLOWUP_PHASES.NOT_HANDLED_YET.getId()) {
			order.setAgentMustAttend(("1").equals(request.getParameter("agentMustAttend")) ? true : false);
			order.setLastCompletedPhaseId(Enums.JOBORDER_FOLLOWUP_PHASES.FIRST_ACTION_TAKEN.getId());
		}

		dbQueryManager.mergeEntity(order);

		if (request.getParameter("showJobOrderReport") == null)
			setMessage(request, new Message("تم الحفظ بنجاح", Message.MESSAGE_TYPE.SUCCESS));

		updateNotificationsThenGoForward(request, response);

	}

}
